#pragma
#include <iostream>
#include "Protocol.hpp"
enum
{
    Div_Zero = 1, // 表示除0错误
    Mod_Zero,     // 模0错误
    Other_Oper,
};
class ServerCal
{
public:
    ServerCal()
    {
    }
    Response CalculartorHelper(const Request &req)
    {
        Response resp(0, 0);
        switch (req.op)
        {
        case '+':
            resp.result_ = req.x + req.y;
            break;
        case '-':
            resp.result_ = req.x - req.y;
            break;
        case '*':
            resp.result_ = req.x * req.y;
            break;
        case '/':
        {
            if (req.y == 0)
                resp.code_ = Div_Zero;
            else
                resp.result_ = req.x / req.y;
        }
        break;
        case '%':
        {
            if (req.y == 0)
                resp.code_ = Mod_Zero;
            else
                resp.result_ = req.x % req.y;
        }
        break;
        default:
            resp.code_ = Other_Oper;
            break;
        }
        return resp;
    }
    // 获取计算完成并封装好报头的报文
    std::string Calculator(std::string &package)
    {
        std::string content;
        bool r = Decode(package, &content);
        if (!r)
            return ""; // 反序列化失败，说明正文长度有丢失，长度不够
        Request req;
        r = req.Deserialize(content);
        if (!r)
            return "";

        content = "";
        Response resp = CalculartorHelper(req);
        resp.Serialize(&content);  // 序列化
        content = Encode(content); // 添加报头
        return content;
    }
    ~ServerCal() {}
};